{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5dd4b5e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "666832a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d9bb6673",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0375b9d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "22dbf45d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a7c57878",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b0756942-5cae-4592-bd5a-88fdfa6a4c19",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(input: String, order: List<Int>): String {\n",
    "    val CMD_LEN = 18\n",
    "    val X_OFFSET = 5\n",
    "    val Y_OFFSET = 15\n",
    "    val Z_OFFSET = 4\n",
    "    \n",
    "    val lines = input.split(\"\\n\")    \n",
    "    val n = lines.size / CMD_LEN\n",
    "    \n",
    "    val a = (0 until n).map { lines[it * CMD_LEN + X_OFFSET].split(\" \")[2].toInt() }\n",
    "    val b = (0 until n).map { lines[it * CMD_LEN + Y_OFFSET].split(\" \")[2].toInt() }\n",
    "    val c = (0 until n).map { if (lines[it * CMD_LEN + Z_OFFSET].split(\" \")[2] == \"26\") 1 else 0 }\n",
    "    \n",
    "    val z = IntArray(1)\n",
    "    val ans = mutableListOf<Int>()\n",
    "\n",
    "    \n",
    "    fun dfs(): Boolean {\n",
    "        val i = ans.size\n",
    "        \n",
    "        if (i == n)\n",
    "            return z[0] == 0\n",
    "        \n",
    "        for (v in order) {\n",
    "            val oz = z[0]\n",
    "                   \n",
    "            val x = if ((z[0] % 26 + a[i]) != v) 1 else 0\n",
    "            if (a[i] < 0 && x == 1)\n",
    "                continue\n",
    "\n",
    "            if (c[i] == 1)\n",
    "                z[0] /= 26\n",
    "\n",
    "            z[0] = (25 * x + 1) * z[0] + (v + b[i]) * x\n",
    "            ans.add(v)\n",
    "            \n",
    "            if (dfs())\n",
    "                return true\n",
    "            \n",
    "            ans.removeAt(ans.size - 1)\n",
    "            \n",
    "            z[0] = oz   \n",
    "        }\n",
    "            \n",
    "        return false\n",
    "    }\n",
    "    \n",
    "    dfs()\n",
    "    \n",
    "    return ans.joinToString(\"\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4b83b1cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    return solve(input, IntArray(9) { 9 - it }.toList())\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3ec5e33e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "59692994994998"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33cee59a",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2504229b",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "    return solve(input, IntArray(9) { 1 + it }.toList())\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "305b0355",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16181111641521"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76496007",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-6372"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
